'''
Created on Jan 17, 2013

@author: dheigl
'''
from PeriodicReturns import PeriodicReturns
from PeriodicReturnsList import PeriodicReturnsList
from Stock import Stock
from StockList import StockList
from fetchPortfolio import GoogleQuote
from IndexedVector import IndexedVector
from IndexedVectorList import IndexedVectorList
from Simulation import Simulation
import random

import math
from fin2012.RandomizedList import RandomizedList
from fin2012.SimulationList import SimulationList

def calculateVAR(portfolio):
    
    for Stock in StockList.stockList:
        print Stock.key
        print Stock.date
        print Stock.quotes
        quotes = Stock.quotes
        pr = PeriodicReturns(Stock.key)
        for i in xrange(0, len(quotes) - 1):
            pr.add((math.log(quotes[i + 1] / quotes[i])))
        PeriodicReturnsList(pr)
        
'''FEHLERQUELLE'''        
def IndexPeriodicReturns():
    rVector = IndexedVectorList()
    
    
    '''
    counter = 0
    for i in xrange(len(PeriodicReturnsList.returnsList[0].value)):
        if i > len(PeriodicReturnsList.returnsList) & counter >= len(PeriodicReturnsList.returnsList):
            counter = 0
        for j in xrange(len(PeriodicReturnsList.returnsList)):
            item = PeriodicReturnsList.returnsList[j].value[i]
            rVector.iVectorList[counter].add(item)
        counter +=1
    print rVector'''
    list = []
    for a in PeriodicReturnsList.returnsList:
            list.append(a.value)
    indexedList = zip(*list)
    print indexedList
    helper = []
    print "\n"
    for i in indexedList:
        for a in i:
            helper.append(a)
    help = 0
    if len(helper) % 2:
        i = len(indexedList)
    else:
        i = len(indexedList) - 1
        help = 1
    start = 0
    end = i
    for anz in xrange(len(PeriodicReturnsList.returnsList) + help):
        index = IndexedVector(anz)
        rVector.add(index)
        for a in helper[start:end]:
            rVector.iVectorList[anz].add(a)
        start = end
        end += i
    for v in rVector.iVectorList:
        print v.value
        
def printList():
    for IndexedVector in IndexedVectorList.iVectorList:
        print "KEY: ", IndexedVector.key
        print "VALUE: ", IndexedVector.value
        
def PrintPeriodicReturns():
    for PeriodicReturns in PeriodicReturnsList.returnsList:
        print PeriodicReturns.key
        print PeriodicReturns.value
        
def randomize(start=0, end=0):
    length = len(IndexedVectorList.iVectorList) - 1
    for i in xrange(end):
        rdm = random.randint(start, length)
        RandomizedList(IndexedVectorList.iVectorList[rdm])
    return RandomizedList

'''##############TESTDATEN##############'''
def randomize1(start=0, end=0):
    RandomizedList(IndexedVectorList.iVectorList[4])
    RandomizedList(IndexedVectorList.iVectorList[4])
    RandomizedList(IndexedVectorList.iVectorList[2])
    RandomizedList(IndexedVectorList.iVectorList[0])
    RandomizedList(IndexedVectorList.iVectorList[1])
    RandomizedList(IndexedVectorList.iVectorList[1])
    RandomizedList(IndexedVectorList.iVectorList[3])
    RandomizedList(IndexedVectorList.iVectorList[1])
    for vector in RandomizedList.rList:
        print vector.key
        print vector.value
    return RandomizedList
            
if __name__ == '__main__':
    symbols = ['GOOG', 'MSFT', 'AMZN']
    data = GoogleQuote().getData(symbols, '2012-01-01')
    calculateVAR(data)
    PrintPeriodicReturns()
    IndexPeriodicReturns()
    printList()
    print "\n"
    simList = SimulationList()
    for i in range(10):
        r = randomize(0, 10)
        s = Simulation()
        s.simulate(r)
        simList.add(s)
        RandomizedList.rList = []
    print ""
